package net.openl10n.mdf;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import net.openl10n.mdf.immutable.ImmutableBuilder;
import net.openl10n.mdf.immutable.ImmutableStateException;

public class DocumentBuilder implements ImmutableBuilder<Document>{

	private DocumentId id;
	private Long version;
	private List<PartId> parts;
	
	public DocumentBuilder() {
		parts = new ArrayList<PartId>();
	}
	
	public DocumentBuilder(Document document) {
		this.id = document.getId();
		this.version = document.getVersion();
		this.parts = new ArrayList<PartId>(document.getParts());
	}

	public DocumentId getId() {
		return id;
	}
	
	public void setId(DocumentId id) {
		this.id = id;
	}
	
	public Long getVersion() {
		return version;
	}
	
	public void setVersion(Long version) {
		this.version = version;
	}
	
	public List<PartId> getParts() {
		return parts;
	}
	
	public void setParts(List<PartId> parts) {
		this.parts = parts;
	}
	
	@Override
	public boolean isValid() {
		return id != null && version != null && parts != null;
	}
	
	public void validate() throws ImmutableStateException {
		if(! isValid()) {
			Set<String> invalidFields = new LinkedHashSet<String>();
			if(id == null)
				invalidFields.add("id");
			if(version == null)
				invalidFields.add("version");
			if(parts == null)
				invalidFields.add("parts");
			throw new ImmutableStateException(invalidFields);
		}
	}
	
	@Override
	public Document build() throws ImmutableStateException {
		validate();
		return new Document(id, version, parts);
	}
}
